<?php
class ControllerAccountContract extends Controller {
	private $error = array();
	public function index() {
		$this->language->load('account/contract');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$contractValue = '1';
			$this->load->model('account/customer');
			$this->model_account_customer->updateContractCustomer($contractValue);
			$this->redirect($this->url->https('account/success'));
		}

		$this->document->title = $this->language->get('heading_title');

		$this->document->breadcrumbs = array();

		$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('common/home'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
		);

		$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('account/account'),
        	'text'      => $this->language->get('text_account'),
        	'separator' => $this->language->get('text_separator')
		);

		$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('account/contract'),
        	'text'      => $this->language->get('text_success'),
        	'separator' => $this->language->get('text_separator')
		);

		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['button_dogovor'] = $this->language->get('button_download');
		$this->data['button_dogovorPdf'] = $this->language->get('button_pdf_download');
		$this->data['text_agree'] = $this->language->get('text_agree');

		if (isset($this->request->post['agree'])) {
			$this->data['agree'] = $this->request->post['agree'];
		} else {
			$this->data['agree'] = FALSE;
		}

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (!$this->config->get('config_customer_approval')) {
			$this->data['text_message'] = sprintf($this->language->get('text_message'), $this->url->http('information/contact'));
		} else {
			$this->data['text_message'] = sprintf($this->language->get('text_approval'), $this->config->get('config_store'), $this->url->http('information/contact'));
		}

		$this->data['dogovor'] = $this->url->https('checkout/confirm/dogovor');
		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['action'] = $this->url->https('account/contract');

		if ($this->cart->hasProducts()) {
			$this->data['continue'] = $this->url->http('checkout/cart');
		} else {
			$this->data['continue'] = $this->url->http('account/success');
		}

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/contract.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/contract.tpl';
		} else {
			$this->template = 'default/template/common/contract.tpl';
		}

		$this->children = array(
			'common/header',
			'common/footer',
			'common/column_left',
			'common/column_right'
			);

			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function validate() {
		$this->language->load('account/contract');
		if (!isset($this->request->post['agree'])) {
			$this->error['warning'] = $this->language->get('error_agree');
		}
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function dogovor() {
		if (true) {
			/*$this->response->addheader('Pragma', 'public');
			$this->response->addheader('Expires', '0');
			$this->response->addheader('Content-Description', 'File Transfer');
			$this->response->addheader('Content-Type', 'application/msword');
			$this->response->addheader('Content-Disposition', 'attachment; filename=shartnoma.doc');
			$this->response->addheader('Content-Transfer-Encoding', 'binary');

			$word = new COM("word.application") or die ("Could not initialise MS Word object.");
			$word->Documents->Open(realpath("shartnoma.doc"));

			// Extract content.
			$content = (string) $word->ActiveDocument->Content;
			if ($this->customer->isLogged()){
				$this->load->model('account/address');
				$shipping_address_id = $this->customer->getAddressId();
				$shipping_address = $this->model_account_address->getAddress($shipping_address_id);
				$data['shipping_firstname'] = $shipping_address['firstname'];
				$data['shipping_lastname'] = $shipping_address['lastname'];
				$data['shipping_company'] = $shipping_address['company'];
				$data['shipping_address_1'] = $shipping_address['address_1'];
				$data['shipping_address_2'] = $shipping_address['address_2'];
				$data['shipping_city'] = $shipping_address['city'];
				$data['shipping_postcode'] = $shipping_address['postcode'];
				$data['shipping_zone'] = $shipping_address['zone'];
				$data['shipping_zone_id'] = $shipping_address['zone_id'];
				$data['shipping_country'] = $shipping_address['country'];
				$data['shipping_country_id'] = $shipping_address['country_id'];
				$data['shipping_address_format'] = $shipping_address['address_format'];


				$content = str_replace("<company>",$this->customer->getCompany(),$content);
				$content = str_replace("<opf>",$this->customer->getOpf(),$content);
				$content = str_replace("<okpo>",$this->customer->getOkpo(),$content);
				$content = str_replace("<okonx>",$this->customer->getOkonx(),$content);
				$content = str_replace("<inn>",$this->customer->getInn(),$content);
			}

			$word->ActiveDocument->Close(false);

			$word->Quit();
			$word = null;
			unset($word);

			$this->response->setOutput($content);*/
			$mypdf = PDF_new();
			PDF_open_file($mypdf, "");
			PDF_begin_page($mypdf, 595, 842);
			$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);
			PDF_setfont($mypdf, $myfont, 10);

			PDF_show_xy($mypdf, 'Contract ..........', 80, 800);

			PDF_end_page($mypdf);
			PDF_close($mypdf);
			$mybuf = PDF_get_buffer($mypdf);
			$mylen = strlen($mybuf);
			$this->response->addheader('Content-type', 'application/pdf');
			$this->response->addheader('Content-Length', '$mylen');
			$this->response->addheader('Content-Disposition', 'inline; filename=dogovorsd.pdf');
			$this->response->setOutput($mybuf);
			PDF_delete($mypdf);
		} else {
			return $this->forward('error/error_404', 'index');
		}
	}
}
?>